package com.wh.wisdomsite.xbox.server.controller.me;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.common.util.date.DateUtil;
import com.wh.wisdomsite.xbox.al.entity.AlarmSign;
import com.wh.wisdomsite.xbox.al.service.IAlarmSignService;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.me.entity.CraneProject;
import com.wh.wisdomsite.xbox.me.service.ICraneProjectService;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 起重机械实时数据展示 Controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.me
 * @date 2018年11月27日 9:38
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/craneDataShow")
public class CraneDataShowController {
    private static final Log logger = LogFactory.getLog(CraneDataShowController.class);

    @Autowired
    private ICraneProjectService iCraneProjectService;

    @Autowired
    private IAlarmSignService iAlarmSignService;


    /**
     * 页面跳转
     * @param request
     * @param prId
     * @return
     */
    @RequiresPermissions("rules:me_craneDataShow_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(HttpServletRequest request,String prId){
        logger.info("加载起重机械设备数据-->>开始");
        Map<String,Object> deviceObjMap = new HashMap<>();
        String deviceId1 = "";
        CraneProject craneProject1 = new CraneProject();
        try{
            EntityWrapper<CraneProject> ewCraneProject = new EntityWrapper<CraneProject>();
            ewCraneProject.setEntity(new CraneProject());
            ewCraneProject.eq("cp.prId", prId);
            List<CraneProject> craneProjects = iCraneProjectService.selectList(ewCraneProject);
            List<Map<String,String>> deviceList = new ArrayList<>();
            List<Map<String,String>> alarmList = new ArrayList<>();
            int i = 0;
            for(CraneProject craneProject : craneProjects){
                Map<String,String> deviceObj = new HashMap<>();
                String deviceId = craneProject.getDeviceId();
                String deviceName = craneProject.getCpName();
                deviceObj.put("deviceId",deviceId);
                deviceObj.put("deviceName",deviceName);
                deviceList.add(deviceObj);
                if(i == 0){
                    deviceId1 = deviceId;
                    craneProject1 = craneProject;
                    i++;
                }
            }
            if(StringUtils.isNotEmpty(deviceId1)){
                alarmList = getCraneAlarm(deviceId1);
            }
            deviceObjMap.put("deviceList",deviceList);
            deviceObjMap.put("alarmList",alarmList);
            deviceObjMap.put("craneProject",craneProject1);
            logger.info("加载起重机械设备数据-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("加载起重机械设备数据-->>异常 Exception" + e.getMessage());
        }
        request.setAttribute("deviceData",deviceObjMap);
        return "/me/craneDataShow/craneDataShow";
    }

    /**
     * 获取起重机械设备警报参数
     * @param deviceId
     * @param response
     */
    @RequestMapping(value = "/craneAlarm.do", method = RequestMethod.GET)
    public void craneAlarm(String deviceId, HttpServletResponse response){
        List<Map<String,String>> alarmList = new ArrayList<>();
        if(StringUtils.isNotEmpty(deviceId)){
            alarmList = getCraneAlarm(deviceId);
        }
        ResManager.getJsonOut(response).print(JsonUtil.Json2All(alarmList));
    }

    /**
     * 获取起重机械警报参数
     * @return
     */
    public List<Map<String,String>> getCraneAlarm(String deviceId){
        //警报内容根据时间倒序选取前五条
        List<Map<String,String>> alarmList = new ArrayList<>();
        if(StringUtils.isNotEmpty(deviceId)){
            Page<AlarmSign> page = new Page<AlarmSign>();
            page.setCurrent(1);
            page.setSize(5);
            EntityWrapper<AlarmSign> ewAlarmSign = new EntityWrapper<AlarmSign>();
            ewAlarmSign.setEntity(new AlarmSign());
            ewAlarmSign.eq("deviceId", deviceId);
            ewAlarmSign.eq("alarmCategory", "1");
            ewAlarmSign.orderBy("createDate",false);
            Page<AlarmSign> alarmSignPage = iAlarmSignService.selectPage(page, ewAlarmSign);
            if (alarmSignPage.getRecords().size() > 0) {
                for(AlarmSign alarmSign : alarmSignPage.getRecords()){
                    Map<String,String> alarmObj = new HashMap<>();
                    String alarmSubdivided = alarmSign.getAlarmSubdivided();
                    String alarmContent = alarmSign.getAlarmContent();
                    Date alarmTime = alarmSign.getAlarmTime();
                    String alarmStatus = alarmSign.getAlarmStatus();
                    String alarmSubdivided1 = alarmSubdivided.substring(alarmSubdivided.lastIndexOf(":")+1);
                    alarmObj.put("alarmSubdivided",alarmSubdivided1);
                    alarmObj.put("alarmContent",alarmContent);
                    alarmObj.put("alarmTime", DateUtil.dateToStringLong(alarmTime));
                    alarmObj.put("alarmStatus",alarmStatus.equals("1")?"未处理":"已处理");
                    alarmList.add(alarmObj);
                }
            }
        }
        return alarmList;
    }

    /**
     * 根据设备id获取起重机械基本信息以及警报信息
     * @param deviceId
     * @param response
     */
    @RequestMapping(value = "/craneProject.do", method = RequestMethod.GET)
    public void getCraneAndAlarm(String deviceId,HttpServletResponse response){
        logger.info("加载起重机械基本数据-->>开始");
        CraneProject craneProject = new CraneProject();
        try{
            if(StringUtils.isNotEmpty(deviceId)){
                EntityWrapper<CraneProject> ewCraneProject = new EntityWrapper<CraneProject>();
                ewCraneProject.setEntity(new CraneProject());
                ewCraneProject.eq("cp.deviceId", deviceId);
                List<CraneProject> craneProjects = iCraneProjectService.selectList(ewCraneProject);
                if(craneProjects.size() > 0){
                    craneProject = craneProjects.get(0);
                }
            }
            logger.info("加载起重机械基本数据-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("加载起重机械基本数据-->>异常 Exception" + e.getMessage());
        }
        ResManager.getJsonOut(response).print(JsonUtil.Json2All(craneProject));
    }
}
